package cn.zcq100.jdbc;
import java.sql.*;
import java.lang.reflect.*;

import cn.zcq100.reflect.User;

public class Demo {
	private final String URL = "jdbc:mysql://localhost:3306/bbs";
	private final String CLASS_NAME = "com.mysql.jdbc.Driver";
	private final String USER = "root";
	private final String PWD = "";

	public Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(CLASS_NAME);
			conn = DriverManager.getConnection(URL, USER, PWD);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return conn;
	}

	public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
		try {
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (stmt != null) {
				stmt.close();
				stmt = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		Demo d = new Demo();
		User u = new User();
		String sql = "select * from tbl_users";
		Connection conn = d.getConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			for (int i = 1; i <= rsmd.getColumnCount(); i++) {
				String str = rsmd.getColumnName(i);
				String methodName = "set" + str.substring(0, 1).toUpperCase()
						+ str.substring(1);
				//u.getClass().getMethod(methodName, d.getType(u, str));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		d.closeAll(conn, stmt, rs);

		
		
	}

	//get ReturnType
	public Class<?> getType(Class c,String filed) {
		Class<?> obj=null;
		for(Method m:c.getMethods()){
			if(m.getName().substring(3).equals(filed)){
				obj=m.getReturnType();
			}
		}
		return obj;
	}
}
